<?php
namespace app\index\controller;

use app\common\mq\Rabbitmq;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use think\facade\Config;
use think\facade\Env;


class Index
{
    public function index()
    {
        echo phpinfo();
        // dump($_ENV);
        // dump(getenv());
        // dump(Env::get());
        // dump(Config::get('cache'));
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    /**
     * 生产者
     */
    public function mq()
    {
        $rabbitmq = new Rabbitmq();
        $res = $rabbitmq->connect();
        
        if($res){
            echo 'connect success!' . "\n";
            $msg = ['key' => 'name', 'value' => 'weihaoyu'];
            $msg = json_encode($msg);
            $rabbitmq->publish($msg, '', 'rpc_queue2');
            echo 'send success：' . $msg . "\n";
        }else{
            echo 'connect error!' . "\n";
        }
    }

    /**
     * 消费者
     */
    public function consume()
    {
        $callBack = function ($message){
            //print_r($message);
            $body = $message->body;
            if(!empty($body)){
                echo "\n" . 'consume success：' . $body . "\n";
                $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
            }
        };
        
        $rabbitmq = new Rabbitmq();
        $res = $rabbitmq->connect();
        $rabbitmq->consumer('', 'rpc_queue2', $callBack);
    }
}
